//
//  KABaseVC.swift
//  kano
//
//  Created by Sophia on 2024/8/14.
//

import UIKit

class KABaseVC: UIViewController, UINavigationControllerDelegate {
    
    //Do not slide back left
    var var_prohibitReturn : Bool = false
   
    var var_hiddenNavigationBar : Bool = true
    
    var var_navType : kaEnumNavBarType = .normal
    
    lazy var var_navBar: KANavView = {
        let var_navBar = KANavView(var_navType)
        var_navBar.ka_gobackBlock({ [weak self] in
            self!.customNavBackAction()
        })
        return var_navBar
    }()
    
    func customNavBackAction() {
        self.navigationController?.popViewController(animated: true)
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        modalPresentationStyle = .fullScreen;
        
        edgesForExtendedLayout = UIRectEdge.init(rawValue: 0)
        extendedLayoutIncludesOpaqueBars = false
        automaticallyAdjustsScrollViewInsets = false
        if #available(iOS 11.0, *) {
        } else {
            edgesForExtendedLayout = UIRectEdge.init(rawValue: 0)
        }
        
        if (var_isBangsScreen) {
            if #available(iOS 11.0, *) {
                additionalSafeAreaInsets = UIEdgeInsets(top: 0, left: 0, bottom: CGFloat(var_safeAreaBottomHeight), right: 0);
            } else {
                // Fallback on earlier versions
            }
        }
        
        view.backgroundColor = .white
        navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.ka_hexColor(0x333333)]
        edgesForExtendedLayout = UIRectEdge(rawValue: 0)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        navigationController?.navigationBar.setBackgroundImage(UIImage.ka_imageWithColor(imgColor: UIColor(0xFFFFFF)), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage.ka_imageWithColor(imgColor: UIColor(0xFFFFFF))
        navigationController?.navigationBar.barTintColor = UIColor.white
        navigationController?.navigationBar.isTranslucent = false
        navigationController?.delegate = self
    }

    
    //MARK: UINavigationControllerDelegate
    
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        if (navigationController.isKind(of: UIImagePickerController.self)) {
            return;
        }
        if (viewController.isKind(of: KABaseVC.self)) {
            navigationController.setNavigationBarHidden((viewController as! KABaseVC).var_hiddenNavigationBar, animated: true)
        } else {
            navigationController.setNavigationBarHidden(true, animated: true)
        }
    }
    
    func ka_showCustomerNavBar(type : kaEnumNavBarType) -> Void {
        var_navType = type
        //hide system navBar
        var_hiddenNavigationBar = true
        view.addSubview(var_navBar)
    }
    
}
